<template>
  <div>
    <el-card style="width: 100%">
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
      >
        <div class="app-space-between">
          <div class="app-space-between-left">
            <el-form-item label="订单编号:" prop="thirdOrderId">
              <el-input
                v-model="queryParams.thirdOrderId"
                placeholder="请输入订单编号"
                clearable
                @clear="handleQuery"
              />
            </el-form-item>
            <el-form-item label="手机号:" prop="phone">
              <el-input
                v-model="queryParams.phone"
                placeholder="请输入手机号"
                clearable
                @clear="handleQuery"
              />
            </el-form-item>
            <el-form-item label="台餐卡号:" prop="mealCardNumber">
              <el-input
                v-model="queryParams.mealCardNumber"
                placeholder="请输入台餐卡号"
                clearable
                @clear="handleQuery"
              />
            </el-form-item>

            <el-form-item label="支付类型:" prop="payType">
              <el-select
                v-model="queryParams.payType"
                placeholder="请选择状态"
                @change="handleQuery"
              >
                <el-option
                  v-for="item in payTypeData"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>

            <el-form-item label="日期:" prop="time">
              <el-date-picker
                v-model="queryParams.time"
                @change="handleQuery"
                value-format="yyyy-MM-dd"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              >
              </el-date-picker>
            </el-form-item>
          </div>
          <div class="app-space-between-right">
            <el-button type="primary" size="mini" @click="handleQuery"
              >搜索</el-button
            >
            <el-button size="mini" @click="resetQuery">重置</el-button>
          </div>
        </div>
      </el-form>

      <el-row class="mb8">
        <el-col :span="1.5" style="margin-right: 10px">
          <el-button type="primary" plain size="mini" @click="handleAdd"
            >新增</el-button
          >
        </el-col>
        <el-col :span="1.5" style="margin-right: 10px">
          <el-button type="primary" plain size="mini" @click="handleEdit"
            >编辑</el-button
          >
        </el-col>
        <el-col :span="1.5" style="margin-right: 10px">
          <el-button type="primary" plain size="mini" @click="handleDel"
            >删除</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button type="primary" plain size="mini" @click="handleExport"
            >导出账单</el-button
          >
        </el-col>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>

      <div style="height: 16px"></div>

      <el-table v-loading="loading" :data="postList">
        <el-table-column
          show-overflow-tooltip
          label="序号"
          type="index"
          width="50"
          align="center"
        />
        <el-table-column
          show-overflow-tooltip
          label="姓名"
          width="140"
          align="center"
          prop="name"
        />
        <el-table-column
          show-overflow-tooltip
          label="手机号"
          width="140"
          align="center"
          prop="phone"
        />
        <el-table-column
          show-overflow-tooltip
          label="台餐卡号"
          width="150"
          align="center"
          prop="mealCardNumber"
        />
        <el-table-column
          show-overflow-tooltip
          label="消费场景"
          width="80"
          align="center"
          prop="purchaseScene"
        >
          <template slot-scope="scope">
            <span>{{
              Number(scope.row.purchaseScene) == 5 ? "小卖部" : "熟食预定"
            }}</span>
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          label="订单编号"
          min-width="140"
          align="center"
          prop="thirdOrderId"
        />
        <el-table-column
          show-overflow-tooltip
          label="支付/退款金额（元）"
          width="200"
          align="center"
          prop="payAmount"
        >
          <template slot-scope="scope">
            <div>
              <div v-if="Number(scope.row.payType) == 1">
                <div>￥{{ scope.row.payAmount }}</div>
                <div class="table-price" style="margin: 10px 0">
                  积分支付：{{ scope.row.integralPayAmount }}
                </div>
                <div class="table-price">
                  微信支付：{{ scope.row.wxPayAmount }}
                </div>
              </div>
              <div v-else>
                <div>￥{{ scope.row.payAmount }}</div>
                <div class="table-price" style="margin: 10px 0">
                  积分退款：{{ scope.row.integralPayAmount }}
                </div>
                <div class="table-price">
                  微信退款：{{ scope.row.wxPayAmount }}
                </div>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          label="支付/退款时间"
          min-width="200"
          align="center"
          prop="payTime"
        />
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
      <template-add ref="template-add" @success="getList"></template-add>
    </el-card>
  </div>
</template>

<script>
import TemplateAdd from "./components/template-add.vue";
const payTypeData = [
  {
    label: "支出",
    value: 1,
  },
  {
    label: "退款",
    value: 2,
  },
];
let listPurchaseHistoryManage = () => {};
let deleteInformation = () => {};
export default {
  components: {
    TemplateAdd,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        thirdOrderId: "",
        phone: "",
        couponTypeEnum: "",
        mealCardNumber: "",
      },
      payTypeData,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询岗位列表 */
    async getList() {
      try {
        this.loading = true;
        let form = {
          ...this.queryParams,
        };
        if (form.time) {
          form.startDate = form.time[0];
          form.endDate = form.time[1];
        }
        delete form.time;
        let res1 = await listPurchaseHistoryManage(form, form);
        this.postList = res1.rows;
        this.total = res1.total;
        this.loading = false;
      } catch (e) {
        this.loading = false;
      }
    },
    /** 新增 */
    handleAdd() {
      this.$refs["template-add"].init();
    },
    /** 编辑 */
    handleEdit(row) {
      if (this.ids.length != 1 && !row.id)
        return this.$message.error("请选择一个数据进行编辑");
      let postIds = row.id || this.ids[0];
      this.$refs["template-add"].init(postIds);
    },
    /** 删除 */
    handleDel(row) {
      let postIds = row.id || this.ids;
      if (this.ids.length != 1 && !row.id)
        return this.$message.error("请选择要删除的数据");
      if (Array.isArray(postIds)) postIds = postIds.toString();
      this.$confirm("确认删除用户吗？删除后，数据不能恢复。", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return deleteInformation(postIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$refs["queryForm"].resetFields();
      this.handleQuery();
    },
    /** 导出按钮操作 */
    async handleExport() {
      this.download(
        "/lgb/purchaseHistory/export",
        {
          ...this.queryParams,
        },
        `老干部消费_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style lang="scss" scoped>
.brother + .brother {
  margin-left: 6px;
}

.table-price {
  text-align: left;
  padding-left: 40px;
}
</style>
